package ru.reaperoq.inventory.screens.main.profile

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.consumeWindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.Logout
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.dp
import models.shared.UserUi

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ProfileScreen(modifier: Modifier = Modifier, user: UserUi, onLogout: () -> Unit) {
    val textColor = if (user.location != null) {
        MaterialTheme.colorScheme.primary
    } else {
        MaterialTheme.colorScheme.error
    }
    val role = remember(user) {
        when (user.role) {
            UserUi.Role.USER -> "Пользователь"
            UserUi.Role.MODERATOR -> "Модератор"
            UserUi.Role.ADMIN -> "Администратор"
        }
    }
    Scaffold(
        topBar = {
            TopAppBar(title = {},
                actions = {
                    IconButton(onClick = onLogout) {
                        Icon(
                            imageVector = Icons.AutoMirrored.Default.Logout,
                            contentDescription = null
                        )
                    }
                })
        }
    ) { padding ->
        Column(
            modifier = modifier
                .consumeWindowInsets(padding)
                .padding(padding)
                .fillMaxSize(),
            verticalArrangement = Arrangement.spacedBy(
                16.dp,
                alignment = Alignment.CenterVertically
            ),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Box(
                modifier = Modifier
                    .size(96.dp)
                    .clip(CircleShape)
                    .background(MaterialTheme.colorScheme.primaryContainer),
                contentAlignment = Alignment.Center
            ) {
                Text(
                    text = user.username[0].toString(),
                    style = MaterialTheme.typography.headlineLarge
                )
            }
            Text(text = user.username, style = MaterialTheme.typography.titleLarge)
            Text(text = buildAnnotatedString {
                append(role)
                withStyle(style = SpanStyle(fontWeight = FontWeight.Bold)) {
                    append(" • ")
                }
                withStyle(style = SpanStyle(color = textColor)) {
                    append(user.location?.name ?: "Без локации")
                }
            })
        }
    }
}